package com.huatai.paas.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huatai.common.api.R;
import com.huatai.common.log.annotation.Log;
import com.huatai.common.log.enums.BusinessType;
import com.huatai.paas.dto.*;
import com.huatai.paas.dto.info.QueryProjectManagementInfo;
import com.huatai.paas.service.ProjectManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <b>ProjectManagementController</b>
 *
 * @description: ProjectManagementController <br>
 * @date: 2024/7/22 09:23 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */


@Api(tags = "项目管理")
@RestController
public class ProjectManagementController {
	private static final Logger logger = LoggerFactory.getLogger(ProjectManagementController.class);

	@Autowired
	private ProjectManagementService projectManagementService;


	@ApiOperation("分页查询项目管理列表")
	@PostMapping("/query/projectManagement/list")
	public R queryProjectManageList(@RequestBody QueryProjectManagementDto dto){
		IPage<QueryProjectManagementInfo> pageInfo = projectManagementService.queryProjectManageList(dto);
		return R.data(pageInfo);
	}

	@ApiOperation("新增项目管理")
	@PostMapping("/add/projectManagement")
	@Log(title = "项目管理",businessType = BusinessType.INSERT)
	public R addProjectManagement(@RequestBody CreateProjectManagementDto dto){
		return projectManagementService.addProjectManagement(dto);
	}

	@ApiOperation("根据ID修改项目管理内容")
	@PostMapping("/update/projectManagement")
	@Log(title = "项目管理",businessType = BusinessType.UPDATE)
	public R updateProjectManagement(@RequestBody UpdateProjectManagementDto dto){
		return projectManagementService.updateProjectManagement(dto);
	}

	@ApiOperation("根据ID删除项目管理内容")
	@GetMapping("/delete/projectManagement")
	@Log(title = "项目管理",businessType = BusinessType.DELETE)
	public R deleteProjectManagement(@RequestParam long id){
		boolean status = projectManagementService.deleteProjectManagement(id);
		return R.data(status);
	}

	@ApiOperation("发布项目")
	@PostMapping("/publish/projectManagement")
	@Log(title = "项目管理",businessType = BusinessType.UPDATE)
	public R publishProjectManagement(@RequestBody PublishProjectManagementDto dto){
		return projectManagementService.publishProjectManagement(dto);
	}

	@ApiOperation("上传封面图片")
	@PostMapping("/upload/cover")
	@Log(title = "项目管理",businessType = BusinessType.UPLOAD)
	public R uploadCoverUrl(@RequestParam("file") MultipartFile file){
		String coverUrl = projectManagementService.uploadCoverUrl(file);
		return R.data(coverUrl);
	}

	@ApiOperation("保存项目配置")
	@PostMapping("/save/itemDisposition")
	@Log(title = "项目管理",businessType = BusinessType.UPDATE)
	public R saveItemDisposition(@RequestBody CreateItemDispositionDto dto){
		boolean status = projectManagementService.saveItemDisposition(dto);
		return R.data(status);
	}

	@ApiOperation("撤销发布")
	@PostMapping("/unpublish/itemDisposition")
	@Log(title = "项目管理",businessType = BusinessType.UPDATE)
	public R unPublishItemDisposition(@RequestBody PublishProjectManagementDto dto){
		boolean status = projectManagementService.unPublishItemDisposition(dto);
		return R.data(status);
	}

	@ApiOperation("根据ID查询项目详情")
	@GetMapping("/query/itemDisposition/info")
	public R<QueryProjectManagementInfo> queryItemDispositionInfo(@RequestParam Long id){
		QueryProjectManagementInfo info = projectManagementService.queryItemDispositionInfo(id);
		return R.data(info);
	}

	@ApiOperation("查询项目下所有场景模型信息")
	@GetMapping("/query/fileList")
	public R queryFileList(@RequestParam(value = "itemId") Long itemId){
		Map<String,Object> map = projectManagementService.queryFileList(itemId);
		return R.data(map);
	}

	@ApiOperation("目标服务器命令执行回执")
	@GetMapping("/project/publish/status")
	public R projectPublishStatus(@RequestParam(value = "itemId") Long itemId,@RequestParam(value = "status") String status){
		return projectManagementService.projectPublishStatus(itemId,status);
	}

	@ApiOperation("根据经纬度信息获取天气数据")
	@GetMapping("/project/weather")
	public R projectWeather(@RequestParam(value = "lat") String lat,@RequestParam(value = "lng") String lng){
		return projectManagementService.projectWeather(lat,lng);
	}

	@ApiOperation("前端调起UE程序回调")
	@PostMapping("/ue/request/check")
	public R ueRequestBack(@RequestBody UeRequestBackDto dto){
		System.out.println("前端调起UE程序回调 requestUrl : " + dto.getRequestUrl());
		System.out.println("前端调起UE程序回调 params : " + dto.getParams());
		return projectManagementService.ueRequestBack(dto);
	}
}

